#include "head.h"

user_t client[10] =
    {
        {"张三", "123", 0, 789, 0},
        {"李四", "345", 1, 891, 0},
        {"王五", "312", 2, 213, 0},
        {"赵六", "679", 3, 783, 0},
        {"墨阳", "666", 3, 999, 0},
        {"1", "1", 1, 999, 0}}; // 定义用户

user_t user = {"墨阳", "1126", 3, 9999}; // 要插入的用户

// 插入链表
void insert_node(u_list *list, user_t data, int pos)
{
    if (list == NULL) // 判断传递过来的list有没有指向
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    u_node *L1 = (u_node *)malloc(sizeof(u_node)); // 创建一个节点，用来当作用户的插入节点
    L1->userm = data;
    L1->next = NULL;
    if (pos == 0) //  把用户数据放入到L1的节点
    {
        list->next = L1;
    }
    else if (pos == 1) // 判断是否头指针插入
    {
        L1->next = list->next; // 让新节点连接到头节点的下一个指针
        list->next = L1;       // 让头指针指向新节点
    }
    else
    {
        u_node *L2 = (u_node *)malloc(sizeof(u_node)); // 创建新节点
        L2 = list->next;                               // 让新节点移到头节点位置

        for (size_t i = 1; i < pos - 1; i++) // 唯一到要插入节点的前一个位置
        {
            L2 = L2->next; // 让L2指向下自己的下一个节点
        }
        L1->next = L2->next; // 让要插入节点指向要插入位置的后一个位置
        L2->next = L1;       // 让插入位置的前指针指向新节点
    }
}
// 遍历输出
void print_u_list(u_list *list)
{
    if (list == NULL) // 判断传递来的指针是非为NULL
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    u_node *L1 = list->next; // 创建节点用来存放用户链表的头节点
    while (L1 != NULL)       // 判断L1的节点是到最后一个
    {
        printf("用户名:%s  密码:%s  信息等级:%d  余额:%d 航班信息%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value, L1->userm.ticket); // 输出用户的值
        L1 = L1->next;                                                                                                                                          // 让L1指向自己的下一个节点
    }
}

// 创建
u_list *init_u_list()
{
    u_list *L1 = (u_list *)malloc(sizeof(u_list)); // 创建头指针
    if (L1 == NULL)                                // 判NULL
    {
        ERRLOG("头指针创建失败");
        return NULL;
    }
    L1->next = NULL; // 给赋初值
    return L1;       // 返回
}

// 用户写入文件
void insert_file_u_node(u_list *list)
{
    if (list == NULL) // 判断链表头节点list是非为空
    {
        ERRLOG("传递来的链表为空");
        return;
    }
    FILE *fp0 = fopen(REGULAR_FILE, "w");  // 打开文件普通用户文件
    FILE *fp1 = fopen(SILVER_FILE, "w");   // 打开文件白银用户文件
    FILE *fp2 = fopen(GOLD_FILE, "w");     // 打开文件黄金用户文件
    FILE *fp3 = fopen(PLATINUM_FILE, "w"); // 打开文件铂金用户文件
    if (fp0 == NULL)                       // 判断四个文件是非打开成功
    {
        ERRLOG("文件fp0打开失败");
        return;
    }
    if (fp1 == NULL)
    {
        ERRLOG("文件fp1打开失败");
        return;
    }
    if (fp2 == NULL)
    {
        ERRLOG("文件fp2打开失败");
        return;
    }
    if (fp3 == NULL)
    {
        ERRLOG("文件fp3打开失败");
        return;
    }
    u_node *L1 = list->next; // 创建节点并移动到头指针
    while (L1 != NULL)       // 判断L1是非到最后一个
    {
        if (L1->userm.card >= 20000)
            L1->userm.IDcard = 3;
        else if (L1->userm.card >= 15000)
            L1->userm.IDcard = 2;
        else if (L1->userm.card >= 10000)
            L1->userm.IDcard = 1;
        else
            L1->userm.IDcard = 0;
            int i;
        switch (L1->userm.IDcard) // 判断该用户是什么类型的用户，把用户写入对应的文件
        {
        case 0:
            fprintf(fp0, "%s\t%s\t%d\t%d\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value, L1->userm.card, L1->userm.ticket);
            for (i = 0; i < L1->userm.ticket; i++)
            {
                fprintf(fp0, "%d\t", L1->userm.order[i].id);
                fprintf(fp0, "%s\t", L1->userm.order[i].flighttype);
                fprintf(fp0, "%s\t", L1->userm.order[i].startcity);
                fprintf(fp0, "%s\t", L1->userm.order[i].arrivecity);
                fprintf(fp0, "%d:%d\t", L1->userm.order[i].starttime[0], L1->userm.order[i].starttime[1]);
                fprintf(fp0, "%d:%d\t", L1->userm.order[i].arrivetime[0], L1->userm.order[i].arrivetime[1]);
                fprintf(fp0, "%s\t", L1->userm.order[i].flytime);
                fprintf(fp0, "%d\t", L1->userm.order[i].value);
                fprintf(fp0, "%d\t", L1->userm.order[i].maxNUM);
                fprintf(fp0, "%d\t", L1->userm.order[i].leftbuyersa);
                fprintf(fp0, "%c\n", L1->userm.order[i].whetherfly);
            }
            break;
        case 1:
            fprintf(fp1, "%s\t%s\t%d\t%d\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value, L1->userm.card, L1->userm.ticket);
            for ( i = 0; i < L1->userm.ticket; i++)
            {
                fprintf(fp1, "%d\t", L1->userm.order[i].id);
                fprintf(fp1, "%s\t", L1->userm.order[i].flighttype);
                fprintf(fp1, "%s\t", L1->userm.order[i].startcity);
                fprintf(fp1, "%s\t", L1->userm.order[i].arrivecity);
                fprintf(fp1, "%d:%d\t", L1->userm.order[i].starttime[0], L1->userm.order[i].starttime[1]);
                fprintf(fp1, "%d:%d\t", L1->userm.order[i].arrivetime[0], L1->userm.order[i].arrivetime[1]);
                fprintf(fp1, "%s\t", L1->userm.order[i].flytime);
                fprintf(fp1, "%d\t", L1->userm.order[i].value);
                fprintf(fp1, "%d\t", L1->userm.order[i].maxNUM);
                fprintf(fp1, "%d\t", L1->userm.order[i].leftbuyersa);
                fprintf(fp1, "%c\n", L1->userm.order[i].whetherfly);
            }
            break;
        case 2:
            fprintf(fp2, "%s\t%s\t%d\t%d\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value, L1->userm.card, L1->userm.ticket);
            for (i = 0; i <L1->userm.ticket; i++)
            {
                fprintf(fp2, "%d\t", L1->userm.order[i].id);
                fprintf(fp2, "%s\t", L1->userm.order[i].flighttype);
                fprintf(fp2, "%s\t", L1->userm.order[i].startcity);
                fprintf(fp2, "%s\t", L1->userm.order[i].arrivecity);
                fprintf(fp2, "%d:%d\t", L1->userm.order[i].starttime[0], L1->userm.order[i].starttime[1]);
                fprintf(fp2, "%d:%d\t", L1->userm.order[i].arrivetime[0], L1->userm.order[i].arrivetime[1]);
                fprintf(fp2, "%s\t", L1->userm.order[i].flytime);
                fprintf(fp2, "%d\t", L1->userm.order[i].value);
                fprintf(fp2, "%d\t", L1->userm.order[i].maxNUM);
                fprintf(fp2, "%d\t", L1->userm.order[i].leftbuyersa);
                fprintf(fp2, "%c\n", L1->userm.order[i].whetherfly);
            }
            break;
        case 3:
            fprintf(fp3, "%s\t%s\t%d\t%d\t%d\t%d\n", L1->userm.name, L1->userm.passwd, L1->userm.IDcard, L1->userm.value, L1->userm.card, L1->userm.ticket);
            for (i = 0; i < L1->userm.ticket; i++)
            {
                fprintf(fp3, "%d\t", L1->userm.order[i].id);
                fprintf(fp3, "%s\t", L1->userm.order[i].flighttype);
                fprintf(fp3, "%s\t", L1->userm.order[i].startcity);
                fprintf(fp3, "%s\t", L1->userm.order[i].arrivecity);
                fprintf(fp3, "%d:%d\t", L1->userm.order[i].starttime[0], L1->userm.order[i].starttime[1]);
                fprintf(fp3, "%d:%d\t", L1->userm.order[i].arrivetime[0], L1->userm.order[i].arrivetime[1]);
                fprintf(fp3, "%s\t", L1->userm.order[i].flytime);
                fprintf(fp3, "%d\t", L1->userm.order[i].value);
                fprintf(fp3, "%d\t", L1->userm.order[i].maxNUM);
                fprintf(fp3, "%d\t", L1->userm.order[i].leftbuyersa);
                fprintf(fp3, "%c\n", L1->userm.order[i].whetherfly);
            }
            break;
        }
        L1 = L1->next; // 用户写入之后，让L1指向下一个节点
    }
    fclose(fp0); // 文件写入后关闭四个文件
    fclose(fp1);
    fclose(fp2);
    fclose(fp3);
}
// 文件读取
void scanf_file_u_node(u_list *list)
{

    char name[128];     // 存放登陆姓名
    char password[128]; // 存放用户密码
    printf("\033[1;31;40m请输入你要登陆的姓名:\033[0m");
    scanf("%s", name);
    printf("\033[1;31;40m请输入你的密码\033[0m");
    scanf("%s", password);
    u_node *L1 = list->next;
    while (L1 != NULL)
    {
        if ((0 == strcmp(L1->userm.name, name)) && (strcmp(L1->userm.passwd, password) == 0)) // 判断密码和用户是非一致
        {
            user_t len_data;
            printf("\033[1;31;40m登陆成功 \033[0m\n");
            sleep(3);
            len_data = test(L1->userm); // 密码正确就跳转菜单
            L1->userm.value = len_data.value;
            L1->userm.ticket = len_data.ticket;
            L1->userm.IDcard = len_data.IDcard;
            L1->userm.card = len_data.card;
              memcpy(L1->userm.order,len_data.order,10*sizeof(f_data));
            strcpy(L1->userm.passwd, len_data.passwd);
            insert_file_u_node(list); // 将用户写入文件
        }
        L1 = L1->next;
    }
}

